package cn.dankal.flyaudio.pop;

import android.content.Context;
import android.media.Image;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.util.image.PicUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.friend.adapter.ShareMemeberAdapter;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetGroupInfoCallback;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.GroupInfo;
import cn.jpush.im.android.api.model.UserInfo;
import razerdp.basepopup.BasePopupWindow;

/**
 * 朋友圈分享弹窗
 */
public class FriendSharePop extends BasePopupWindow implements View.OnClickListener {

    private ImageView picView;

    private View groupLayout;
    private View listLayout;
    private RecyclerView memebersListView;
    private ImageView groupAvatarView;
    private TextView groupNameView;
    private ShareMemeberAdapter adapter;
    private EditText contentEditText;
    /**
     * 私聊用户Id
     */
    private String targetId = "";
    /**
     * 群组Id
     */
    private long groupId = 0;

    /**
     * 分享的文件图片
     */
    private File file;


    public FriendSharePop(Context context) {
        super(context);
    }

    public FriendSharePop setGroupId(long groupId) {
        this.groupId = groupId;
        groupLayout.setVisibility(View.VISIBLE);
        Conversation conversation = JMessageClient.getGroupConversation(groupId);
        JMessageClient.getGroupInfo(groupId, new GetGroupInfoCallback() {
            @Override
            public void gotResult(int i, String s, GroupInfo groupInfo) {
                Glide.with(getContext()).load(groupInfo.getAvatarFile()).placeholder(R.mipmap.ic_default_avatar).into((groupAvatarView));
                groupNameView.setText(groupInfo.getGroupName());
            }
        });
        return this;
    }

    public FriendSharePop setTargetId(String targetId) {
        this.targetId = targetId;
        groupLayout.setVisibility(View.VISIBLE);
        Conversation conversation = JMessageClient.getSingleConversation(targetId);
        JMessageClient.getUserInfo(targetId, new GetUserInfoCallback() {
            @Override
            public void gotResult(int i, String s, UserInfo userInfo) {
                Glide.with(getContext()).load(userInfo.getAvatarFile()).placeholder(R.mipmap.ic_default_avatar).into((groupAvatarView));
                groupNameView.setText(userInfo.getNickname());
            }
        });
        return this;
    }

    public FriendSharePop setConversation(List<Conversation> conversation) {
        this.targetId = targetId;
        groupLayout.setVisibility(View.GONE);
        listLayout.setVisibility(View.VISIBLE);
        adapter.addDatas(conversation, true);
        return this;
    }

    public FriendSharePop setShareFile(File file) {
        this.file = file;
        Picasso.with(getContext()).load(file).into(picView);
        return this;
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.pop_friend_share);
    }


    @Override
    public void onViewCreated(View contentView) {
        super.onViewCreated(contentView);
        picView = findViewById(R.id.iv_pic);
        groupAvatarView = findViewById(R.id.iv_group_avatar);
        groupNameView = findViewById(R.id.tv_group_name);
        groupLayout = findViewById(R.id.rl_group_layout);
        memebersListView = findViewById(R.id.recyclerview);
        contentEditText = findViewById(R.id.tv_content);
        memebersListView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        adapter = new ShareMemeberAdapter();
        memebersListView.setAdapter(adapter);
        listLayout = findViewById(R.id.rl_list_layout);
        findViewById(R.id.tv_send).setOnClickListener(this);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_send:
                if (listener != null) {
                    dismiss();
                    listener.doSend(contentEditText.getText().toString().trim());
                }
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
        }
    }

    private DoSendListener listener;

    public FriendSharePop setDoSendListener(DoSendListener listener) {
        this.listener = listener;
        return this;
    }

    public interface DoSendListener {

        void doSend(String content);

    }
}
